<?php

/**
 * Framework_Presenter_Smarty_SearchBar
 *
 * @author Application Support Group
 * @copyright University of Georgia
 * @license http://www.opensource.org/licenses/gpl-license.php
 * @package BAMF
 * @filesource
*/


/**
 * Framework_Presenter_Smarty_SearchBar
 *
 *
 * @author Application Support Group
 * @package BAMF
 * @link http://smarty.php.net
*/
class Framework_Presenter_Smarty_SearchBar extends Framework_Presenter_Smarty_Extension {

	public function __construct() {
		parent::__construct() ;
	}

	public static function register( &$smartyObj ) {
		$smartyExtension = new Framework_Presenter_Smarty_SearchBar() ;
		$smartyObj->register_function( "searchBar", array( $smartyExtension, "searchBar" )) ;
	}


// build a search bar for the given collection, format parameters, and state

	public function searchBar($params, &$smarty)
	{
		$_output = array() ;
		
		extract($params);
	
		if (empty($collection)) {
			$smarty->trigger_error("searchBar: missing 'collection' parameter");
			return;
		}

		if (empty($format)) {
			$smarty->trigger_error("searchBar: missing 'format' parameter");
			return;
		}

		

		if(empty($get)) {
			$get = "";
		} else {
			$get = "?".$get;
		}

		$collectionParameters = $collection->getParameters() ;
		$searchState = unserialize( $collectionParameters->rawSearchParams ) ;
		
		// also allow for supplementalData, which would be optional
		
		// and lookupData...is this required?

// We use the fields defined in the collection schema unless they're being overridden by the format array
		
		$fields = count( $format[ 'fields' ]) > 0 ? $format[ 'fields' ] : $collection->_schema[ 'fields' ] ;
		$modulePath = $smarty->get_template_vars( 'modulePath' ) ;
		$itemNumber = $smarty->get_template_vars( 'itemNumber' ) ;
		$formAction = empty( $itemNumber ) ? $modulePath : $modulePath . "/" . $itemNumber ;

		if(isset($basehref) && !empty($basehref)) {
			$formAction = $basehref . $formAction;
		}

// data types for each column these drive the (dis)appearance of the various value form elements
		
		$_output[] = '<script language="javascript" type="text/javascript">' ;
		$_output[] = '<!--' ;
		$_output[] = '	var dataType = new Array();' ;

		foreach ( $collection->_schema['field_definitions'] as $fieldKey => $fieldDef ) {
			if ( isset( $format[ 'isSearchable' ][ $fieldKey ] ) && $format[ 'isSearchable' ][ $fieldKey ] ) {
				if ( !empty( $collection->_schema[ 'pk_foreign_keys' ][ $fieldKey ] ) || ( isset( $format[ 'customLookup' ][ $fieldKey ] ) && !empty( $format[ 'customLookup' ][ $fieldKey ] ))) {
					$_output[] = "dataType['" . $fieldKey . "'] = '" . $fieldKey . "';" ;
				} else {
					$_output[] = "dataType['" . $fieldKey . "'] = '" . $fieldDef . "';" ;
				}
			}
		}

		$_output[] = '	var activeElement = "";	' ;
		$_output[] = '	function hideAllSearchValues() {' ;
	
		foreach ( $collection->_schema[ 'field_definitions' ] as $fieldKey => $fieldDef ) {
			$_output[] = "setElementDisplay('" . $fieldKey . "', 'none');" ;
		}

		$_output[] = '		setElementDisplay("strInput", "none");' ;
		$_output[] = '		setElementDisplay("dateInput", "none");' ;
		$_output[] = '	}' ;

	
	
		$_output[] = "	function resetValues() {
				srchFrm = document.forms['". SBAR_FORM_NAME . "'];
				srchFrm.elements['dateInput_0'].value = '';
				srchFrm.elements['dateInput_1'].value = '';
				srchFrm.elements['strInput'].value = '';
		
				for (i = 0; i < srchFrm.elements.length; i++) {
					elementName = srchFrm.elements[i].name ;
					if (dataType[elementName] && dataType[elementName] == elementName) {
						srchFrm.elements[elementName].selectedIndex = 0 ;
					}
				}
			}" ;
	
		$_output[] = "
			function showValueElement(thisSelect) {
				thisCol = thisSelect.options[thisSelect.selectedIndex].value;
		
				hideAllSearchValues();		
				switch(dataType[thisCol]) {
					case 'int':
						setElementDisplay('strInput', 'inline');
						activeElement = 'strInput';
						break;
					case 'xml':
						setElementDisplay('strInput', 'inline');
						activeElement = 'strInput';
						break;
					case 'string':
						setElementDisplay('strInput', 'inline');
						activeElement = 'strInput';
						break;
					case 'date':
						setElementDisplay('dateInput', 'inline');
						activeElement = 'dateInput';
						break;
					case 'money':
						setElementDisplay('strInput', 'inline');
						activeElement = 'strInput';
						break;
					case 'phone':
						setElementDisplay('strInput', 'inline');
						activeElement = 'strInput';
						break;
					case 'email':
						setElementDisplay('strInput', 'inline');
						activeElement = 'strInput';
						break;
					case 'key':
						setElementDisplay('strInput', 'inline');
						activeElement = 'strInput';
						break;
					case 'virtual':
						setElementDisplay('strInput', 'inline');
						activeElement = 'strInput';
						break;
					default:
						setElementDisplay(thisCol, 'inline');
						activeElement = thisCol;
						break;
				}
			}
		" ;

		$_output[] = "
			function setElementDisplay(elementId, displayType) {
				thisElement = document.getElementById(elementId);
				if (thisElement != null) thisElement.style.display = displayType;
			}
			" ;
			
		$_output[] = "	
			function setHiddenFormElements() {
				srchFrm = document.forms['" . SBAR_FORM_NAME. "'];
				if (activeElement == 'dateInput') {
					srchFrm.elements['" . SBAR_KEY_SEARCH_0 . "'].value = srchFrm.elements['dateInput_0'].value;
					srchFrm.elements['" . SBAR_KEY_SEARCH_1 . "'].value = srchFrm.elements['dateInput_1'].value;
				} else {
					srchFrm.elements['" . SBAR_KEY_SEARCH_1 . "'].value = '';
					if (activeElement == 'strInput') {
						srchFrm.elements['" . SBAR_KEY_SEARCH_0 . "'].value = srchFrm.elements['strInput'].value;
					} else {
						srchFrm.elements['" . SBAR_KEY_SEARCH_0 . "'].value = srchFrm.elements[activeElement].options[srchFrm.elements[activeElement].selectedIndex].value;
					}			
				}
			}
			" ;
			
		
		$_output[] = '-->' ;
		$_output[] = "</script>" ;
	
		$_output[] = '<div class="searchBar">' ;
			
		$_output[] = '	<form name="'. SBAR_FORM_NAME. '" onsubmit="setHiddenFormElements();" method="post" action="'. $formAction . $get . '">' ;
		$_output[] = '		<input type="hidden" name="' . SBAR_KEY_SEARCH_0 . '" value="" />' ;
		$_output[] = '		<input type="hidden" name="' . SBAR_KEY_SEARCH_1 . '" value="" />' ;

		if( isset( $formSupplement ) && !empty( $formSupplement )) {
			$_output[] = $formSupplement ;
		}
		
		if( isset( $supplementalData ) && count( $supplementalData ) > 0 ) {
			foreach ( $supplementalData as $key => $value ) {
				$_output[] = '<input type="hidden" name="' . $key . '" value="' . $value . '" />' ;
			}
		}
				
		$_output[] = "<p>Search:" ;
		$_output[] = '<select name="' . SBAR_KEY_SEARCH_COLUMN . '" onchange="resetValues();showValueElement(this);">' ;
		
		foreach ( $fields as $field ) {
			if ( isset( $format[ 'isSearchable' ][ $field ]) && $format[ 'isSearchable' ][ $field ] ) {
				$selected = "" ;
				if ( $searchState[ 'search_column' ][0] == $field ) {
					$selected = ' selected="selected"' ;
				}
				$_output[] = '<option value="' . $field .'"' . $selected . '>' ;
				
				if ( isset( $format[ 'altSearchLabel' ][ $field ] ) && !empty( $format[ 'altSearchLabel' ][ $field ] )) {
					$_output[] = $format[ 'altSearchLabel' ][ $field ] ;
				} elseif( isset( $format[ 'colHeader' ][ $field ] ) && !empty( $format[ 'colHeader' ][ $field ] )) {
					$_output[] = $format[ 'colHeader' ][ $field ] ;
				} else {
					$_output[] = $collection->_schema[ 'field_aliases' ][ $field ] ;
				}
				
				$_output[] = '</option>' ;
			}
		}
		$_output[] = "</select>" ;
		
		
		foreach( $fields as $field ) {
			if( isset( $format[ 'customLookup' ][ $field ] ) && !empty( $format[ 'customLookup' ][ $field ] )) {
				$_output[] = '<span id="' . $field . '" style="display:inline">' ;
				$_output[] = " is " ;
				$_output[] = '<select name="' . $field . '">' ;
				
				foreach( $format[ 'customLookup' ][ $field ] as $key => $value ) {
					$selected = "" ;
					if( $searchState[ 'search_value_0' ][0] == $key ) {
						$selected = ' selected="selected"' ;
					}
					$_output[] = '<option value="' . $key . '"' . $selected . '>' . $value . '</option>' ;
				}
				
				$_output[] = '</select>' ;
				$_output[] = '</span>' ;
			} elseif ( isset( $collection->_schema[ 'pk_foreign_keys' ][ $field ] ) && !empty( $collection->_schema[ 'pk_foreign_keys' ][ $field ] )) {
				$_output[] = '<span id="' . $field . '" style="display:inline">' ;
				$_output[] = " is " ;
				$_output[] = '<select name="' . $field . '">' ;
				
				if( !empty( $lookupData ) && isset( $lookupData[ $field ] ) && !empty( $lookupData[ $field ] )) {
					foreach( $lookupData[ $field ] as $key => $value ) {
						$selected = "" ;
						if( $searchState[ 'search_value_0' ][0] == $key ) {
							$selected = ' selected="selected"' ;
						}
						$_output[] = '<option value="' . $key . '"' . $selected . '>' . $value . '</option>' ;
					}
				}
				
				$_output[] = '</select>' ;
				$_output[] = '</span>' ;
			}
		}
		
			
		$_output[] = '			 
				 <span id="strInput" style="display:inline">
					 is
			<input type="text" name="strInput" value="' . htmlspecialchars($searchState[ "search_value_0" ][0]) . '" />
				 </span>
				
				<span id="dateInput" style="display:inline">
					 from:
					 <input type="text" name="dateInput_0" size="14" value="' . $searchState[ "search_value_0" ][0] . '" onfocus="showCalendarControl(this);" />
					 to:
					 <input type="text" name="dateInput_1" size="12" value="' . $searchState[ "search_value_1" ][0] . '" onfocus="showCalendarControl(this);" />
				 </span>
				 
				 &nbsp;&nbsp;<input type="submit" name="' . SBAR_KEY_SUBMIT . '" value="' . SBAR_VAL_SUBMIT . '" />
				 &nbsp;&nbsp;<input type="submit" name="' . SBAR_KEY_RESET . '" value="' . SBAR_VAL_RESET . '" />
				 </p>
			</form>
		</div>
		<script language="javascript" type="text/javascript">
		showValueElement(document.forms["' . SBAR_FORM_NAME . '"].elements["' . SBAR_KEY_SEARCH_COLUMN . '"]);
		</script>
		' ;


		return implode( "\n", $_output ) ;


	}

	public function __destruct() {

	}


}
